﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections.Specialized;
using System.Drawing;
using System.Drawing.Design;
//using Dirso.Articles.Controls.Design;
using Dirso.Articles.Controls;

namespace Dirso.Articles.Controls
{
    /// <summary>
    /// An enhanced TextBox control
    /// </summary>
    [ToolboxData("<{0}:textboxex runat=server></{0}:textboxex>"),
    DefaultProperty("Text"), ToolboxBitmap(typeof(TextBox))]
    public class TextBoxEx : System.Web.UI.WebControls.TextBox
    {
        #region Static Const Values
        private const string ScriptName = "DirsoTextBoxExScript";
        #endregion

        #region Designer Properties
        /// <summary>
        /// The font color setted when the control gets the focus
        /// </summary>
        //[Description("The font color setted when the control gets the focus"),
        //Category("Appearance"),
        //Editor(typeof(System.Drawing.Design.ColorEditor), typeof(UITypeEditor))]
        public virtual Color FontColorEnter
        {
            get
            {
                object color = ViewState["FontColorEnter"];
                if (color == null)
                    return Color.FromArgb(ForeColor.ToArgb());
                else
                    return (Color)color;
            }
            set
            {
                ViewState["FontColorEnter"] = value;
            }
        }

        /// <summary>
        /// The font color setted when the control shows the ToolTip
        /// </summary>
        //[Description("The font color setted when the control shows the ToolTip"),
        //Category("Appearance"),
        //Editor(typeof(System.Drawing.Design.ColorEditor), typeof(UITypeEditor))]
        public virtual Color FontColorToolTip
        {
            get
            {
                object color = ViewState["FontColorToolTip"];
                if (color == null)
                    return Color.FromArgb(192, 192, 192);
                else
                    return (Color)color;
            }
            set
            {
                ViewState["FontColorToolTip"] = value;
            }
        }

        /// <summary>
        /// The control's color setted when the control gets the focus
        /// </summary>
        //[Description("The control's color setted when the control gets the focus"),
        //Category("Appearance"),
        //Editor(typeof(System.Drawing.Design.ColorEditor), typeof(UITypeEditor))]
        public virtual Color BackColorEnter
        {
            get
            {
                object color = ViewState["BackColorEnter"];
                if (color == null)
                    return Color.FromArgb(BackColor.ToArgb());
                else
                    return (Color)color;
            }
            set
            {
                ViewState["BackColorEnter"] = value;
            }
        }

        /// <summary>
        /// The control's color setted when the control loses the focus
        /// </summary>
        //[Description("The control's color setted when the control gets the focus"),
        //Category("Appearance"), DefaultValue(typeof(Color), "#FFFFFF"),
        //Editor(typeof(System.Drawing.Design.ColorEditor), typeof(UITypeEditor))]
        public override Color BackColor
        {
            get
            {
                object color = ViewState["BackColor"];
                if (color == null)
                    return Color.White;
                else
                    return (Color)color;
            }
            set
            {
                ViewState["BackColor"] = value;
            }
        }

        /// <summary>
        /// The label that should have it's properties changed when the user focus this control
        /// </summary>
        //[Description("The label that should have it's properties changed when the user focus this control"),
        //Category("Data"), Themeable(false),
        //Editor(typeof(SelectLabelEditor), typeof(UITypeEditor))]
        public virtual string Label
        {
            get
            {
                object label = ViewState["Label"];
                if (label == null)
                    return string.Empty;
                else
                    return (string)label;
            }
            set
            {
                ViewState["Label"] = value;
            }
        }
        #endregion

        #region Holder Properties
        /// <summary>
        /// Holds the original fore color value
        /// </summary>
        protected Color OldForeColor;
        /// <summary>
        /// Holds the Client ID of the label
        /// </summary>
        protected string LabelID = null;
        #endregion

        #region Override Methods
        /// <summary>
        /// Initializes the control
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            //
            Page.RegisterRequiresViewStateEncryption();
        }

        /// <summary>
        /// Loads default values if needed
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            OldForeColor = ForeColor;
            if (string.IsNullOrEmpty(Text))
            {
                Text = ToolTip;
                ForeColor = FontColorToolTip;
            }

            // script codes
            if (!Page.ClientScript.IsClientScriptBlockRegistered(ScriptName))
                RegisterScript();
        }

        /// <summary>
        /// Finds the label ID, so the script can be registered properly
        /// </summary>
        /// <param name="cc"></param>
        /// <returns></returns>
        private string GetID(ControlCollection cc)
        {
            foreach (Control c in cc)
            {
                if (c is Label)
                {
                    if (c.ID.ToLower() == Label.ToLower())
                    {
                        return c.ClientID;
                    }
                }
                if (c.HasControls())
                {
                    string s = GetID(c.Controls);
                    if (!string.IsNullOrEmpty(s)) return s;
                }
            }
            return null;
        }

        /// <summary>
        /// Overrides the render method
        /// </summary>
        /// <param name="output"></param>
        protected override void Render(HtmlTextWriter output)
        {
            //
            if (!string.IsNullOrEmpty(Label))
            {
                LabelID = GetID(Page.Controls);
            }

            // inner methods
            if (!string.IsNullOrEmpty(LabelID))
            {
                output.AddAttribute("onblur",
                    "JRTextBox_ChangeStyles_Exit_Label('" +
                    LabelID + "'," + ClientID + ",'" + ToolTip + "','" +
                    ColorTranslator.ToHtml(FontColorToolTip) + "','" +
                    ColorTranslator.ToHtml(BackColor) + "','" +
                    ColorTranslator.ToHtml(OldForeColor) +
                    "');"
                    );
                output.AddAttribute("onfocus",
                    "JRTextBox_ChangeStyles_Enter_Label('" +
                    LabelID + "'," + ClientID + ",'" + ToolTip + "','" +
                    ColorTranslator.ToHtml(BackColorEnter) + "','" +
                    ColorTranslator.ToHtml(FontColorEnter) +
                    "');"
                    );
            }
            else
            {
                output.AddAttribute("onblur",
                    "JRTextBox_ChangeStyles_Exit(" +
                    ClientID + ",'" + ToolTip + "'," +
                    ColorTranslator.ToHtml(FontColorToolTip) + "," +
                    ColorTranslator.ToHtml(BackColor) + "," +
                    ColorTranslator.ToHtml(OldForeColor) +
                    ");"
                    );
                output.AddAttribute("onfocus",
                    "JRTextBox_ChangeStyles_Enter(" +
                    ClientID + ",'" + ToolTip + "'," +
                    ColorTranslator.ToHtml(BackColorEnter) + "," +
                    ColorTranslator.ToHtml(FontColorEnter) +
                    ");"
                    );
            }
            // calls the base method
            base.Render(output);
        }
        #endregion

        #region private methods

        private void RegisterScript()
        {
            #region Main Script Code
            string script = @"
<script type='text/javascript'>
function JRTextBox_ChangeStyles_Enter(id, hint, backcolor, fontcolor)
{
    if(id.value==hint)id.value='';
    id.style.color = fontcolor;
    id.style.backgroundColor = backcolor;
}

function JRTextBox_ChangeStyles_Enter_Label(label, id, hint, backcolor, fontcolor)
{
    JRTextBox_ChangeStyles_Enter(id, hint, backcolor, fontcolor);
    elem = document.getElementById(label);
    if(elem!=null)
    {
        elem.style.fontWeight = 'bold';
    }
}

function JRTextBox_ChangeStyles_Exit(id, hint, hintfontcolor, backcolor, fontcolor)
{
    if(id.value=='')
    {
        id.value=hint;
        id.style.color = hintfontcolor;
        id.style.backgroundColor = backcolor;
    }
    else
    {
        id.style.color = fontcolor;
        id.style.backgroundColor = backcolor;
    }
}

function JRTextBox_ChangeStyles_Exit_Label(label, id, hint, hintfontcolor, backcolor, fontcolor)
{
    JRTextBox_ChangeStyles_Exit(id, hint, hintfontcolor, backcolor, fontcolor);
    elem = document.getElementById(label);
    if(elem!=null)
    {
        elem.style.fontWeight = 'normal';
    }
}

function JRTextBox_ValidateHint(id, hint)
{
    elem = document.getElementById(id);
    if(elem!=null)
    {
        if(elem.value==hint)elem.value='';
    }
}
</script>";
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), ScriptName, script);
            #endregion

            #region OnSubmit
            // register the script to avoid the hint to be posted
            Page.ClientScript.RegisterOnSubmitStatement(this.GetType(),
                string.Format("{0}_{1}_OnSubmit", ScriptName, ID),
                string.Format("JRTextBox_ValidateHint('{0}','{1}');\n", ClientID, ToolTip)
                );
            #endregion
        }
        #endregion
    }
}
